package com.demo2do.strawberry.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.strawberry.entity.user.UserRegister;
import com.demo2do.strawberry.entity.user.member.Member;
import com.demo2do.strawberry.entity.user.profile.Profile;

/**
 * @author Wilson
 */
@Repository("userDao")
public class UserDao {
	
	@Autowired
	private GenericDaoSupport genericDaoSupport;

	@SuppressWarnings("unchecked")
	public Profile getUserProfile(Member member) {
		List<Profile> profiles = genericDaoSupport.searchForCacheableList("FROM Profile profile WHERE profile.member = :member", "member", member);
		return profiles.isEmpty() ? null : profiles.get(0);
	}
		
	@SuppressWarnings("unchecked")
	public List<UserRegister> getUserRegister(String email, boolean enabled) {
		
		String hql = "FROM UserRegister userRegister WHERE userRegister.email = :email ";

		if(enabled)
			hql += "and userRegister.enabled = true";

		return genericDaoSupport.searchForCacheableList(hql, "email", email);
	}

}
